<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>layui</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link href="../../../static/font-awesome-4.7.0/css/font-awesome.min.css" rel="stylesheet">
	<link rel="stylesheet" href="../../../static/vui/css/vui.css">

	<script type="text/javascript" src="../../../static/js/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="../../../static/js/jquery.cookie.min.js"></script>
	<script type="text/javascript" src="../../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/vui.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/treegrid.js"></script>
	<script type="text/javascript" src="../../../static/js/common.js"></script>
</head>
<body style="overflow-y: scroll;">

<div id="table"></div>

<script>
    var params = app.getRequestParams();
    $(function(){
        function getOpenFrame(){
            if( params.index ==- 1 ){
                return parent;
			}else{
                return parent.frames[0];
			}
        }

        $('#table').treegrid({
			check:true,
            url:app.BASE_API_URL+'menu/find',
            root:-1,
			width:330,
            columns:[
                {field:'name',title:'菜单名称',width:150}
            ],
            loadSuccess:function () {
                app.request({
                    url:'role/findMenus',
                    data:{roleId:params.id},
                    success:function (resp) {
                        $.each(resp.data,function (i,row) {
                            $('input[type="checkbox"][value="'+row.menuId+'"]').prop('checked',true);
                        });
                    }
                });
            }
        });

        app.getOkBtn().click(function(){

            var data = {roleId:params.id};
            var index = 0;
            $('input[type="checkbox"]').each(function () {
                var checked = $(this).prop('checked');
                if( checked ){
                    data['menuIds['+index+']'] = $(this).val();
                    index++;
				}
            });

            app.request({
                url:'role/authority',
                data:data,
                success:function (resp) {
                    if( resp.result==0 ){
                        app.closeLastWindow();
                        app.message(parent.layer,'操作成功');
                        parent.$("#table").treegrid("reload");
                    }else{
                        app.message(parent.layer,'操作失败【'+app.getErrorMsg(resp)+'】',2);
                    }
                }
            });

        });
    });

</script>
</body>
</html>